#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
void* get_v(void*);
void* calculate_v(void*);
typedef struct
{
	int data;
	sem_t get_sem,c_sem;
}Data;
int main(void)
{
	Data data = {1};
	sem_init(&data.get_sem,0,0);
	sem_init(&data.c_sem,0,0);
	pthread_t thread1_id,thread2_id;
	// get_v线程先读取输入，calculate_v线程计算，再由get_v线程输出
	pthread_create(&thread1_id,NULL,calculate_v,(void*)&data);
	pthread_create(&thread2_id,NULL,get_v,(void*)&data);
	pthread_join(thread1_id,NULL);
	pthread_join(thread2_id,NULL);
	return 0;
}
void* get_v(void* data)
{
	Data* d = (Data*)data;
	scanf("%d",&d->data);
	sem_post(&d->c_sem);
	sem_wait(&d->get_sem);
	printf("data = %d\n",d->data);
	return (void*)NULL;
}
void* calculate_v(void* data)
{
	Data* d = (Data*)data;
	sem_wait(&d->c_sem);
	d->data += 8;
	sem_post(&d->get_sem);
	return (void*)NULL;
}
